package com.itany.springcloud.springcloudnacosopenfeignconsumer.fallback;

import com.itany.springcloud.springcloudcommon.entity.User;
import com.itany.springcloud.springcloudcommon.vo.ResponseResult;
import com.itany.springcloud.springcloudnacosopenfeignconsumer.service.UserService;
import com.itany.springcloud.springcloudnacosopenfeignconsumer.service.fallback.UserServiceFallback;
import feign.FeignException;
import feign.hystrix.FallbackFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

@Component
public class UserServiceFallbackFactory implements FallbackFactory<UserService> {

    @Autowired
    private UserServiceFallback userServiceFallback;

    @Override
    public UserService create(Throwable throwable) {
        System.out.println(throwable.getClass()+"----------------------");
        FeignException e= (FeignException) throwable;
        System.out.println(e.status()+"----------------------");
        if(401==e.status()) {
            return new UserService(){
                @Override
                public ResponseResult findAll() {
                    return new ResponseResult("401","未登录",null);
                }

                @Override
                public ResponseResult findById(Integer id) {
                    return new ResponseResult("401","未登录",null);
                }

                @Override
                public ResponseResult add(String username, String password) {
                    return new ResponseResult("401","未登录",null);
                }

                @Override
                public ResponseResult add2(User user) {
                    return new ResponseResult("401","未登录",null);
                }

                @Override
                public ResponseResult add3(User user) {
                    return new ResponseResult("401","未登录",null);
                }

                @Override
                public ResponseResult remove(Integer id) {
                    return new ResponseResult("401","未登录",null);
                }

                @Override
                public ResponseResult modify(Integer id, String username, String password) {
                    return new ResponseResult("401","未登录",null);
                }
            };
        }
        return userServiceFallback;
    }
}
